<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/10/24
 */

namespace app\common\utils;

class JsonService
{
    private static $SUCCESS_DEFAULT_MSG = '操作成功';

    private static $ERROR_DEFAULT_MSG = '操作失败';

    public static function result($code, $msg = '', $data = [], $count = 0)
    {
        return json(compact('code', 'msg', 'data', 'count'));
    }


    public static function success($msg = 'success',$data = [],$status = 200)
    {
        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESS_DEFAULT_MSG;
        }
        return self::result($status, $msg, $data);
    }


    public static function error($msg,$data = [],$code = 300)
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$ERROR_DEFAULT_MSG;
        }
        return self::result($code, $msg, $data);
    }

    public static function fail($msg, $data = [], $code = 400)
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$ERROR_DEFAULT_MSG;
        }
        return self::result($code, $msg, $data);
    }

}